#!/usr/bin/env python

# Python code to display all email addresses an account will receive email for
# Author: William Cannon
# Date: 11.25.2007

import gdata.apps.service
import gdata.service

try:
    # create a gdata apps service
    service = gdata.apps.service.AppsService(
    email='',
    domain='',
    password='' 
    )

    # log into google apps programmatically
    service.ProgrammaticLogin()

    # Retrieve all users
    myUserFeed = service.RetrieveAllUsers()
    
    #print myUserFeed: print username, and all email addresses for the user
    myEntryList = myUserFeed.entry
    print "Number of accounts is:", len(myEntryList)

    # first we get their nicknames
    # then we get their list memberships
    for a in myEntryList:
        print a.login.user_name + ": ",
        nicknames = service.RetrieveNicknames(a.login.user_name)
        nicknamelist = nicknames.entry
        
        for b in nicknamelist:
            print b.nickname.name + " ",        

        #getting all the email lists the user is a member of
        myEmailListFeed = service.RetrieveEmailLists(a.login.user_name)
        mylist = myEmailListFeed.entry
        for c in mylist:
            print c.email_list.name,

        print "\n"

except gdata.service.RequestError, e:
        raise AppsForYourDomainException(e.args[0])
